package com.zx.utils.web;

import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class Pagination<T> {
	
	public Pagination(int index,int pageSize,int total) {
		this.index=index;
		this.pageSize=pageSize;
		this.total=total;
		setTotalPage();
	}
	
	// 总共的数据量
    private int total;
 
    // 每页显示多少条
    private int pageSize;
 
    // 共有多少页
    private int totalPage;
 
    // 当前是第几页
    private int index;
 
    // 数据
    private List<T> data;

	private JSONObject jsonObject;

    // 分页HTML
    private String pageStr;	
    
	public String getPageStr() {
		StringBuffer sb = new StringBuffer();
		if (total > 0) {
			// 每页数量
			sb.append("	<span>每页数量</span>\n");
			sb.append("	<select id=\"psize\" onchange=\"changeCount()\">\n");
			sb.append("	<option value='10' "+(pageSize==10 ? "selected='selected'" : "")+" >10</option>\n");
			sb.append("	<option value='50' "+(pageSize==50 ? "selected='selected'" : "")+" >50</option>\n");
			sb.append("	<option value='100' "+(pageSize==100 ? "selected='selected'" : "")+" >100</option>\n");
			sb.append("	</select>\n");
			
			// 分页情报
			if (index == 1) {
				sb.append("	<button type=\"button\">上一页</button>\n");
//				sb.append("	<button type=\"button\">下一页</button>\n");
			} else {
				sb.append("	<button type=\"button\" onclick=\"nextPage(1,"+pageSize+")\">首页</button>\n");
				sb.append("	<button type=\"button\" onclick=\"nextPage("+(index-1)+","+pageSize+")\">上一页</button>\n");
			}
			int showTag = 5;//分页标签显示数量
			int startTag = 1;
			if (index > showTag) {
				startTag = index-1;
			}
			int endTag = startTag+showTag-1;
			for (int i=startTag; i<=totalPage && i<=endTag; i++) {
				if (index == i) {
					sb.append(" <button type=\"button\" style=\"color:red\">"+i+"</button>\n");
				} else {
					sb.append(" <button type=\"button\" onclick=\"nextPage("+i+","+pageSize+")\">"+i+"</button>\n");
				}
			}
			if (index == totalPage) {
				sb.append("	<button type=\"button\">下一页</button>\n");
				sb.append("	<button type=\"button\">尾页</button>\n");
			} else {
				sb.append("	<button type=\"button\" onclick=\"nextPage("+(index+1)+","+pageSize+")\">下一页</button>\n");
				sb.append("	<button type=\"button\" onclick=\"nextPage("+getTotalPage()+","+pageSize+")\">尾页</button>\n");
			}
			sb.append("	<span>共<span>"+getTotalPage()+"</span>页\n");
			sb.append("	<span>"+total+"</span>个结果</span>\n");
			
			sb.append("<script type=\"text/javascript\">\n");

		}
		pageStr = sb.toString();
		return pageStr;
	}
    
    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }
 
    public void setIndex(int index)
    {
        this.index = index;
    }
 
    public int getPageSize()
    {
        return pageSize;
    }
 
    public int getTotalPage()
    {
        return (this.total + this.pageSize - 1) / this.pageSize;
    }
 
    public void setTotalPage()
    {
        this.totalPage = (this.total + this.pageSize - 1) / this.pageSize;
    }
 
    public int getIndex()
    {
        return index;
    }
 
    public List<?> getData()
    {
        return data;
    }
 
    public void setData(List<T> data)
    {
        this.data = data;
    }
    
    public int getTotal()
    {
        return total;
    }
 
    public void setTotal(int total)
    {
        this.total = total;
    }

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}


	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
}
